<template>
	<div id="right">
		<div v-if="isLogin" class="logn_reg">
			<div class="logn_reg_div">
				<el-form :label-position="labelPosition" status-icon :rules="rules" ref="userInfo" label-width="100px" :model="userInfo">
					<el-form-item label="账号:" prop="userName" class="ddd">
						<el-input v-model="userInfo.userName"></el-input>
					</el-form-item>
					<el-form-item label="密码:" prop="passWord">
						<el-input type="password" v-model="userInfo.passWord"></el-input>
					</el-form-item>
					<el-form-item>
						<el-button type="primary" @click="submitForm('userInfo',true)">登录</el-button>
						<el-button type="info" @click="close('userInfo')">关闭</el-button>
					</el-form-item>
				</el-form>
			</div>
		</div>
		<div v-if="isReg" class="logn_reg">
			<div class="logn_reg_div">
				<el-form :label-position="labelPosition" status-icon :rules="rules" ref="userInfo" label-width="100px" :model="userInfo">
					<el-form-item label="账号:" prop="userName">
						<el-input v-model="userInfo.userName"></el-input>
					</el-form-item>
					<el-form-item label="昵称:" prop="nickName">
						<el-input v-model="userInfo.nickName"></el-input>
					</el-form-item>
					<el-form-item label="密码:" prop="passWord">
						<el-input type="password" v-model="userInfo.passWord"></el-input>
					</el-form-item>
					<el-form-item label="确认密码:" prop="passWordT">
						<el-input type="password" v-model="userInfo.passWordT"></el-input>
					</el-form-item>
					<el-form-item>
						<el-button type="primary" @click="submitForm('userInfo',false)">注册</el-button>
						<el-button type="info" @click="close('userInfo')">关闭</el-button>
					</el-form-item>
				</el-form>
			</div>
		</div>
		<router-view></router-view>
	</div>
</template>

<script type="application/ecmascript-6">
import Foot from "../foot";

export default {
  name: "right",
  data() {
    var checkUserName = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("邮箱不能为空"));
      }
      var re = /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/;
      if (!re.test(value)) {
        return callback(new Error("邮箱格式不正确"));
      }
      callback();
    };
    var checkNickName = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("昵称不能为空"));
      }
      var reg = /^[\u4E00-\u9FA5]+$/;
      if (!reg.test(value)) {
        if (value.length > 6) {
          return callback(new Error("汉字加子母，或纯字母不能超过6个字符"));
        } else {
          callback();
        }
      } else {
        if (value.length > 4) {
          return callback(new Error("纯汉字不得超过4个字符"));
        } else {
          callback();
        }
      }
    };
    var checkPassWord = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("请输入密码"));
      }
      if (value.length < 6) {
        return callback(new Error("密码不能小于6位"));
      }
      callback();
    };
    var checkPassWord2 = (rule, value, callback) => {
      if (value !== this.userInfo.passWord) {
        return callback(new Error("两次密码不一致"));
      }
      callback();
    };
    return {
      labelPosition: "right",
      userInfo: {
        userName: null,
        passWord: null,
        passWordT: null,
        nickName: null
      },
      rules: {
        userName: [{ validator: checkUserName, trigger: "blur" }],
        nickName: [{ validator: checkNickName, trigger: "blur" }],
        passWord: [{ validator: checkPassWord, trigger: "blur" }],
        passWordT: [{ validator: checkPassWord2, trigger: "blur" }]
      }
    };
  },
  components: {
    Foot
  },
  computed: {
    isLogin: function() {
      return this.$store.getters.getIsLogin;
    },
    isReg: function() {
      return this.$store.getters.getIsReg;
    }
  },
  methods: {
    submitForm(userInfo, type) {
      this.$refs[userInfo].validate(valid => {
        if (valid && type) {
          //登录
          this.$http.post("/api/home", this.userInfo).then(
            res => {
              var dataJ = res.body;
              if (dataJ.code === 1) {
                this.$message.error(dataJ.msg);
                return false;
              }
              //登录成功操作
              // 1.保存token到 前端缓存
              localStorage.setItem("token", JSON.stringify(dataJ.data));
              // 2.一定是为全局可操作
              this.$message({
                message: dataJ.msg,
                type: "success"
              });
              // 3.更新 left页面信息
              this.$store.commit("showUserInfo", dataJ.data.userInfo);
              // 4.关闭登录
              this.$store.commit("showLogin", false);
              this.$refs[userInfo].resetFields();
            },
            error => {
              this.$message.error(
                "程序异常:" + error.status + "，请联系管理员"
              );
            }
          );
        }
        if (valid && !type) {
          //注册
          this.$http
            .post("/api/home/reg", null, {
              params: {
                userName: this.userInfo.userName,
                passWord: this.userInfo.passWord,
                passWordT: this.userInfo.passWordT
              }
            })
            .then(
              res => {
                var dataJ = res.body;
                if (dataJ.code === 1) {
                  this.$message.error(dataJ.msg);
                  return false;
                }
                this.$message({
                  message: dataJ.msg,
                  type: "success"
                });
                this.$store.commit("showReg", false);
                this.$refs[userInfo].resetFields();
              },
              error => {
                this.$message.error(
                  "程序异常:" + error.status + "，请联系管理员"
                );
              }
            );
        }
      });
    },
    close: function() {
      this.userInfo = {};
      this.$store.commit("showLogin", false);
      this.$store.commit("showReg", false);
    }
  }
};
</script>

<style lang="stylus" rel="stylesgeet/stylus" >
#right
  .logn_reg
    position:absolute
    width:100%
    height:100vh
    z-index:30
    .logn_reg_div
      background-color:rgba(0,0,0,0.5)
      padding:40px 50px 20px 0
      margin-top:10%
      margin-left:15%
      width:40%
    .el-form-item__label
      color:#fff
  #home,#blog,#work
    width:100%
    height:100vh
  #foot
    height:40px
    background: url('../../../static/img/footer-bg.png') repeat
    position:fixed
    bottom:0
    width:80%
    padding-top:20px
</style>
